﻿using System;
using System.Collections.Generic;
using PPI.Entities.Distribution;
using PPI.Data;
using System.Data;
using System.Data.SqlClient;

namespace PPIDataAccess.Distribution
{
    public class DPole
    {
        public static DataSet Select(string connectionKey)
        {
            SqlDataAdapter da = null;

            try
            {
                da = new SqlDataAdapter("up_tb_Vendor_Distribution_Poles_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception exc)
            {
                throw new Exception("Error executing up_tb_Vendor_Distribution_Poles_SelectAll", exc);
            }
            finally
            {
                if (da != null)
                    da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(ENote note, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Results_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Pole

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                #endregion

                #region Phase

                if (note.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, Convert.ToInt32(note.Key)));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionResult_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionResult_Id"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();

                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Update(List<ENote> notes, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Results_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Delete actual notes

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, poleId));

                cmd.ExecuteNonQuery();

                #endregion

                #region Insert new notes

                foreach (ENote note in notes)
                {
                    cmd = new SqlCommand("up_tb_Vendor_Distribution_Results_Insert", tr.Connection);
                    cmd.Transaction = tr;

                    cmd.CommandType = CommandType.StoredProcedure;

                    if (poleId == Int32.MinValue)
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                    else
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                    if (note.Id == Int32.MinValue)
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, DBNull.Value));
                    else
                        cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, note.Id));

                    cmd.ExecuteNonQuery();
                }

                #endregion

                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void Delete(Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Poles_Delete", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Delete actual notes

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, poleId));

                cmd.ExecuteNonQuery();

                #endregion

                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void DeleteImages(int[] poleImagesIds, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                SqlConnection connection = DBHelper.GetConnection(connectionKey);
                tr = connection.BeginTransaction();

                foreach (int poleImageId in poleImagesIds)
                {
                    cmd = new SqlCommand("up_tb_Vendor_Distribution_Images_Delete", tr.Connection);
                    cmd.Transaction = tr;
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@imageId", ParameterDirection.Input, true, poleImageId));

                    cmd.ExecuteNonQuery();
                }

                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static int Insert(EDistributionPole pole, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Poles_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Year

                if (pole.Map.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MapFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MapFk", ParameterDirection.Input, true, pole.Map.Id));
                #endregion

                #region Treatment
                if (pole.Section.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SectionFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SectionFk", ParameterDirection.Input, true, pole.Section.Id));
                #endregion

                #region Specie

                if (pole.Specie.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SpecieFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SpecieFk", ParameterDirection.Input, true, pole.Specie.Id));

                #endregion

                #region Class
                
                if (pole.Class.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ClassFK", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ClassFK", ParameterDirection.Input, true, pole.Class.Id));
                
                #endregion

                #region Length

                if (pole.Length.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@LengthFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@LengthFk", ParameterDirection.Input, true, pole.Length.Id));
                
                #endregion

                #region Treatment

                if (pole.Treatment.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@TreatmentFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@TreatmentFk", ParameterDirection.Input, true, pole.Treatment.Id));
                
                #endregion

                cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, pole.Latitude));
                cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, pole.Longitude));

                #region Year
                
                if (pole.Year == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Year", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Year", ParameterDirection.Input, true, pole.Year));
                
                #endregion

                #region Number

                if (string.IsNullOrEmpty(pole.Number))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, pole.Number));
                
                #endregion

                #region Number

                if (string.IsNullOrEmpty(pole.InspBy))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@InspBy", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@InspBy", 100, ParameterDirection.Input, true, pole.InspBy));
                
                #endregion

                #region DistributionType

                if (pole.DistributionType != DistributionType.None )
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@DistributionType", ParameterDirection.Input, true, (Int32)pole.DistributionType));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@DistributionType", ParameterDirection.Input, true, DBNull.Value));
                    
                #endregion

                cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@AcquisitionDate", ParameterDirection.Input, false, pole.AcquisitionDate));

                if (pole.LVisit != DateTime.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@LVisit", ParameterDirection.Input, false, pole.LVisit));
                else
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@LVisit", ParameterDirection.Input, false, DBNull.Value));

                if (pole.GuardsQuantity != Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@GuardsQuantity", ParameterDirection.Input, false, pole.GuardsQuantity));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@GuardsQuantity", ParameterDirection.Input, false, DBNull.Value));

                cmd.Parameters.Add(DBHelper.CreateBitParameter("@PoleTag", ParameterDirection.Input, false, pole.PoleTag));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@GroundWireRepair",ParameterDirection.Input, false, pole.GroundWireRepair));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@Billable", ParameterDirection.Input, false, pole.Billable));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionPole_PoleId", ParameterDirection.ReturnValue, false, 0));
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@D804", 50, ParameterDirection.Input, false, pole.D804));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Inspector", ParameterDirection.Input, false, pole.Inspector.Id));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor", ParameterDirection.Input, false, pole.Vendor.Id));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Project", ParameterDirection.Input, false, pole.Project.Id));

                if (pole.Kv != null && pole.Kv.Id != 0 && pole.Kv.Id != Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@idKv", ParameterDirection.Input, false, pole.Kv.Id));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@idKv", ParameterDirection.Input, true, DBNull.Value));

                if (pole.ConductorType != null && pole.ConductorType.Id != 0 && pole.ConductorType.Id != Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@idConductorType", ParameterDirection.Input, false, pole.ConductorType.Id));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@idConductorType", ParameterDirection.Input, true, DBNull.Value));

                if (pole.ACSR != null && pole.ACSR.Id != Int32.MinValue && pole.ACSR.Id != 0)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@idACSR", ParameterDirection.Input, false, pole.ACSR.Id));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@idACSR", ParameterDirection.Input, true, DBNull.Value));

                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Circuit", 100, ParameterDirection.Input, false, pole.Circuit));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionPole_PoleId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static int Insert(EDistributionPoleImage image, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Images_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Pole

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(image.FileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, image.FileName));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionImage_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionImage_Id"].Value;
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();

                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static void ImageUpdate(Int32 imageId, string fileName, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Images_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Pole

                if (imageId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@vendor_Distribution_Images_ImageId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@vendor_Distribution_Images_ImageId", ParameterDirection.Input, true, imageId));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(fileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, fileName));

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }
        
        public static void MeterImageUpdate(EDistributionPoleImage image, Int32 meterId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_MeterImage_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Meter

                if (meterId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Meters_MeterId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Meters_MeterId", ParameterDirection.Input, true, meterId));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(image.FileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, image.FileName));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionMeterImage_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static Int32 TransformerImageInsert(EDistributionPoleImage image, Int32 transformerId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_TransformerImage_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Meter

                if (transformerId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Transformers_TransformerId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Transformers_TransformerId", ParameterDirection.Input, true, transformerId));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(image.FileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, image.FileName));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionImage_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionImage_Id"].Value;
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();

                return Int32.MinValue;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }


        public static void TransformerImageUpdate(int idImage, string fileName, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_TransformerImage_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Meter

                if (idImage == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ImageId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ImageId", ParameterDirection.Input, true, idImage));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(fileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, fileName));

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static Int32 SectionalizerImageInsert(EDistributionPoleImage image, Int32 sectionalizerId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_SectionalizerImage_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Meter

                if (sectionalizerId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Sectionalizers_SectionalizerId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Sectionalizers_SectionalizerId", ParameterDirection.Input, true, sectionalizerId));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(image.FileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, image.FileName));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionImage_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionImage_Id"].Value;
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();

                return Int32.MinValue;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }


        public static void SectionalizerImageUpdate(int idImage, string fileName, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_SectionalizerImage_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Meter

                if (idImage == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ImageId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ImageId", ParameterDirection.Input, true, idImage));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(fileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, fileName));

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static Int32 RecloserImageInsert(EDistributionPoleImage image, Int32 recloserId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_RecloserImage_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Meter

                if (recloserId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Reclosers_RecloserId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Reclosers_RecloserId", ParameterDirection.Input, true, recloserId));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(image.FileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, image.FileName));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionImage_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionImage_Id"].Value;
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();

                return Int32.MinValue;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }


        public static void RecloserImageUpdate(int idImage, string fileName, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_RecloserImage_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Meter

                if (idImage == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ImageId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ImageId", ParameterDirection.Input, true, idImage));

                #endregion

                #region Phase

                if (String.IsNullOrEmpty(fileName))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@filename", 100, ParameterDirection.Input, true, fileName));

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception exc)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static int Update(EDistributionPole pole, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_Poles_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Pole to update

                if (pole.ServerID == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@PoleId", ParameterDirection.Input, true, pole.ServerID));

                #endregion

                #region Year

                if (pole.Map.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MapFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@MapFk", ParameterDirection.Input, true, pole.Map.Id));
                #endregion

                #region Treatment
                if (pole.Section.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SectionFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SectionFk", ParameterDirection.Input, true, pole.Section.Id));
                #endregion

                #region Specie

                if (pole.Specie.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SpecieFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@SpecieFk", ParameterDirection.Input, true, pole.Specie.Id));

                #endregion

                #region Class

                if (pole.Class.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ClassFK", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@ClassFK", ParameterDirection.Input, true, pole.Class.Id));

                #endregion

                #region Length

                if (pole.Length.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@LengthFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@LengthFk", ParameterDirection.Input, true, pole.Length.Id));

                #endregion

                #region Treatment

                if (pole.Treatment.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@TreatmentFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@TreatmentFk", ParameterDirection.Input, true, pole.Treatment.Id));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Latitude", ParameterDirection.Input, false, pole.Latitude));
                cmd.Parameters.Add(DBHelper.CreateFloatParameter("@Longitude", ParameterDirection.Input, false, pole.Longitude));

                #region Year

                if (pole.Year == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Year", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Year", ParameterDirection.Input, true, pole.Year));

                #endregion

                #region Number

                if (string.IsNullOrEmpty(pole.Number))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Number", 100, ParameterDirection.Input, true, pole.Number));

                #endregion

                #region Number

                if (string.IsNullOrEmpty(pole.InspBy))
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@InspBy", 100, ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@InspBy", 100, ParameterDirection.Input, true, pole.InspBy));

                #endregion

                #region DistributionType

                if (pole.DistributionType != DistributionType.None)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@DistributionType", ParameterDirection.Input, true, (Int32)pole.DistributionType));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@DistributionType", ParameterDirection.Input, true, DBNull.Value));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@AcquisitionDate", ParameterDirection.Input, false, pole.AcquisitionDate));

                if (pole.LVisit != DateTime.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@LVisit", ParameterDirection.Input, false, pole.LVisit));
                else
                    cmd.Parameters.Add(DBHelper.CreateDateTimeParameter("@LVisit", ParameterDirection.Input, false, DBNull.Value));

                if (pole.GuardsQuantity != Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@GuardsQuantity", ParameterDirection.Input, false, pole.GuardsQuantity));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@GuardsQuantity", ParameterDirection.Input, false, DBNull.Value));

                cmd.Parameters.Add(DBHelper.CreateBitParameter("@PoleTag", ParameterDirection.Input, false, pole.PoleTag));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@GroundWireRepair", ParameterDirection.Input, false, pole.GroundWireRepair));
                cmd.Parameters.Add(DBHelper.CreateBitParameter("@Billable", ParameterDirection.Input, false, pole.Billable));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionPole_PoleId", ParameterDirection.ReturnValue, false, 0));
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@D804", 50, ParameterDirection.Input, false, pole.D804));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Inspector", ParameterDirection.Input, false, pole.Inspector.Id));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor", ParameterDirection.Input, false, pole.Vendor.Id));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Project", ParameterDirection.Input, false, pole.Project.Id));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@idKv", ParameterDirection.Input, false, pole.Kv.Id));
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@Circuit", 100, ParameterDirection.Input, false, pole.Circuit));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionPole_PoleId"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }
    }
}
